package com.example.zyfypt_611cgt.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zyfypt_611cgt.R;
import com.example.zyfypt_611cgt.iface.RegisterListener;
import com.example.zyfypt_611cgt.model.RegisterModel;

public class RegisterActivity extends AppCompatActivity {
    private EditText etuser, etpass,etpass2,etname,ettel;
    private Button btncamer,btnregitster,btnreturn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initViews();
    }

    private RegisterListener listener = new RegisterListener() {
        @Override
        public void onResponse(String registermsg) {
            System.out.println("----注册结果：" + registermsg);
            if(registermsg.contains("1")){
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            }else if (registermsg.contains("已存在")){
                Toast.makeText(RegisterActivity.this, "注册失败，用户名已存在", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("----注册失败：" + msg);

            Toast.makeText(RegisterActivity.this, "注册失败：" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private void initViews() {
        etuser = findViewById(R.id.etuser2);
        etpass = findViewById(R.id.etpass2);
        etpass2 = findViewById(R.id.etpass3);
        etname = findViewById(R.id.etname);
        ettel = findViewById(R.id.ettel);
        btncamer = findViewById(R.id.button3);
        btnregitster = findViewById(R.id.button2);
        btnreturn = findViewById(R.id.button4);

        btnregitster.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });
        
        btncamer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(RegisterActivity.this, "功能待开发", Toast.LENGTH_SHORT).show();
            }
        });

        btnreturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void register() {
        String struser = etuser.getText().toString();
        String strpass = etpass.getText().toString();
        String strpass2 = etpass2.getText().toString();
        String strname = etname.getText().toString();
        String strtel = ettel.getText().toString();

        if(TextUtils.isEmpty(struser)){
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if(TextUtils.isEmpty(strpass)){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if(TextUtils.isEmpty(strpass2)){
            Toast.makeText(this, "确认密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if(TextUtils.isEmpty(strtel)){
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if(!strpass.equals(strpass2)){
            Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        RegisterModel registerModel = new RegisterModel();
        registerModel.getRegisterResult(struser,strpass,strtel,listener);
    }
}